﻿using Basic.FormDAL.Model;
using Basic.FormModel.Column;
using Basic.FormModel.DB;
using Basic.FormModel.Model;
using SqlSugar;
using WeDonekRpc.Client;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;
using WeDonekRpc.SqlSugar;

namespace Basic.FormDAL.Repository
{
    internal class TableColumnDAL : BasicDAL<DBTableColumn, long>, ITableColumnDAL
    {
        public TableColumnDAL ( IRepository<DBTableColumn> basicDAL ) : base(basicDAL)
        {
        }
        public long Add ( ColumnAdd data )
        {
            DBTableColumn add = data.ConvertMap<ColumnAdd, DBTableColumn>();
            add.Id = IdentityHelper.CreateId();
            this._BasicDAL.Insert(add);
            return add.Id;
        }

        public long Add ( ColFullAdd data )
        {
            DBTableColumn add = data.ConvertMap<ColFullAdd, DBTableColumn>();
            add.Id = IdentityHelper.CreateId();
            this._BasicDAL.Insert(add);
            return add.Id;
        }
        public ColumnSort[] GetSorts ( long tableId, long groupId )
        {
            ISugarQueryable<ColumnSort>[] tables = new ISugarQueryable<ColumnSort>[]
            {
                this._BasicDAL.Queryable.Where(a=>a.TableId==tableId && a.GroupId==groupId).Select(a=>new ColumnSort
                {
                    Id = a.Id,
                    IsGroup=false,
                    Sort=a.Sort
                }),
                this._BasicDAL.Provider.Queryable<DBTableGroup>().Where(a=>a.TableId==tableId && a.ParentId==groupId).Select(a=>new ColumnSort
                {
                    Id = a.Id,
                    IsGroup=true,
                    Sort=a.Sort
                })
            };
            return this._BasicDAL.Provider.UnionAll<ColumnSort>(tables).OrderBy(a => a.Sort).ToArray();
        }

        public int GetMaxSort ( long tableId, long groupId )
        {
            ISugarQueryable<MaxSortDto>[] tables = new ISugarQueryable<MaxSortDto>[]
            {
                this._BasicDAL.Queryable.Where(a=>a.TableId==tableId && a.GroupId==groupId).Select(a=>new MaxSortDto
                {
                    Sort=a.Sort
                }),
                this._BasicDAL.Provider.Queryable<DBTableGroup>().Where(a=>a.TableId==tableId && a.ParentId==groupId).Select(a=>new MaxSortDto
                {
                    Sort=a.Sort
                })
            };
            return this._BasicDAL.Provider.UnionAll<MaxSortDto>(tables).Max(a => a.Sort);
        }

        public void SetColSpan ( long id, int span )
        {
            if ( !this._BasicDAL.Update(a => a.ColSpan == span, a => a.Id == id) )
            {
                throw new ErrorException("form.table.column.set.fail");
            }
        }

        public void SetColSpan ( KeyValuePair<long, int>[] span )
        {
            ISqlQueue<DBTableColumn> queue = this._BasicDAL.BeginQueue();
            span.ForEach(c =>
            {
                queue.UpdateOneColumn(a => a.ColSpan == c.Value, a => a.Id == c.Key);
            });
            _ = queue.Submit();
        }

        public void SetGroupId ( long id, long groupId )
        {
            if ( !this._BasicDAL.Update(a => a.GroupId == groupId, a => a.Id == id) )
            {
                throw new ErrorException("form.table.column.set.fail");
            }
        }
        public void SetSort ( ColumnSort sour, ColumnSort to )
        {
            ISqlQueue<DBTableColumn> queue = this._BasicDAL.BeginQueue();
            if ( sour.IsGroup )
            {
                queue.UpdateByOneColumn<DBTableGroup>(a => a.Sort == sour.Sort, a => a.Id == sour.Id);
            }
            else
            {
                queue.UpdateOneColumn(a => a.Sort == sour.Sort, a => a.Id == sour.Id);
            }
            if ( to.IsGroup )
            {
                queue.UpdateByOneColumn<DBTableGroup>(a => a.Sort == to.Sort, a => a.Id == to.Id);
            }
            else
            {
                queue.UpdateOneColumn(a => a.Sort == to.Sort, a => a.Id == to.Id);
            }
            _ = queue.Submit();
        }
        public void SetSort ( KeyValuePair<long, int>[] sort )
        {
            ISqlQueue<DBTableColumn> queue = this._BasicDAL.BeginQueue();
            sort.ForEach(c =>
            {
                queue.UpdateOneColumn(a => a.Sort == c.Value, a => a.Id == c.Key);
            });
            _ = queue.Submit();
        }
        public ColSet GetColSet ( long id )
        {
            return this._BasicDAL.JoinGet<DBCustomTable, ColSet>(( a, b ) => a.TableId == b.Id && a.Id == id, ( a, b ) => new ColSet
            {
                Id = a.Id,
                TableType = b.TableType,
                DataType = a.DataType,
                ColType = a.ColType,
                FuncType = a.FuncType,
                ControlSet = a.ControlSet,
            });
        }
        public ColSet[] GetsColSet ( long[] ids )
        {
            return this._BasicDAL.Join<DBCustomTable, ColSet>(( a, b ) => a.TableId == b.Id && ids.Contains(a.Id), ( a, b ) => new ColSet
            {
                Id = a.Id,
                TableType = b.TableType,
                DataType = a.DataType,
                ColType = a.ColType,
                FuncType = a.FuncType,
                ControlSet = a.ControlSet,
            });
        }
    }
}
